<?php
namespace app\app\util;
use think\Request;
use think\cache\driver\Redis;

//排行榜有序集合
define('INTEGRAL','ft:int');
//用户每日获取的积分数
define('USER_INT','ft:user:int:');

class IntUtils{

	//创建用户积分数
	public static function foundUserInt($user_id,$number=0)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		//创建用户
		return $redis->zadd(INTEGRAL,$number,$user_id);
	}

	//添加用户积分数/获取用户积分数
	public static function getUserInt($user_id,$number=0)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($number == 0){
			return $redis->zscore(INTEGRAL,$user_id);
		}else{
			$result = $redis->zincrby(INTEGRAL,$number,$user_id);
			return $result;
		}
	}

	//获取指定范围排名
	public static function getIntRank($start,$end,$type='')
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($type == ''){
			return $redis->zrevrange(INTEGRAL,$start,$end);
		}else{
			$type = 'withscores'; 
			return $redis->zrevrange(INTEGRAL,$start,$end,$type);
		}
	}

	//用户每日获取积分
	public static function getUserDayInt($user_id,$integral=0,$overtime=0)
	{
		$redis = new Redis(['host'=>config('REDIS_HOST'),'password'=>config('REDIS_AUTH')]);
		if($integral == 0){
			return $redis->get(USER_INT.$sid);
		}else{
			return $redis->set(USER_INT.$sid,$overtime);
		}
	}
	
}